/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.dromara.hmily.demo.springcloud.inventory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.log4j.Log4j2;
import org.dromara.hmily.annotation.HmilyTCC;
import org.dromara.hmily.demo.springcloud.inventory.dto.InventoryDTO;
import org.dromara.hmily.demo.springcloud.inventory.entity.InventoryEntity;
import org.dromara.hmily.demo.springcloud.inventory.mapper.InventoryMapper;
import org.dromara.hmily.demo.springcloud.inventory.service.InventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * InventoryServiceImpl.
 *
 * @author xiaoyu
 */
@Service("inventoryService")
@Log4j2
public class InventoryServiceImpl implements InventoryService {


    private final InventoryMapper inventoryMapper;

    @Autowired(required = false)
    public InventoryServiceImpl(InventoryMapper inventoryMapper) {
        this.inventoryMapper = inventoryMapper;
    }


    @Override
    @HmilyTCC(confirmMethod = "confirmMethod", cancelMethod = "cancelMethod")
    @Transactional(rollbackFor = Exception.class)
    public void mockWithTryException(InventoryDTO inventoryDTO) {
        System.out.println("=======执行库存操作开始===========");
        QueryWrapper<InventoryEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(InventoryEntity::getProductId, inventoryDTO.getProductId());
        InventoryEntity oldInventory = inventoryMapper.selectOne(qw);
        oldInventory.setTotalInventory(oldInventory.getTotalInventory() - inventoryDTO.getCount());
        inventoryMapper.updateById(oldInventory);
////        //这里是模拟异常所以就直接抛出异常了
//        throw new HmilyRuntimeException("库存扣减异常！");
    }

    public void confirmMethod(InventoryDTO inventoryDTO) {
        log.info("==========执行库存操作成功===========");
    }

    public void cancelMethod(InventoryDTO inventoryDTO) {
        log.info("==========执行库存操作失败，进行手动回滚===========");
        QueryWrapper<InventoryEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(InventoryEntity::getId, "1");
        InventoryEntity oldInventory = inventoryMapper.selectOne(qw);
        oldInventory.setTotalInventory(oldInventory.getTotalInventory() + inventoryDTO.getCount());
        inventoryMapper.updateById(oldInventory);
    }

}
